﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace E02
{
    class ReflectUtil
    {
        /// <summary>
        /// 设置相应属性的值
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="fieldName">属性名</param>
        /// <param name="fieldValue">属性值</param>
        public static void setValue(object entity, string fieldName, string fieldValue)
        {
            Type entityType = entity.GetType();

            FieldInfo propertyInfo = entityType.GetField(fieldName);

            if (isType(propertyInfo.FieldType, "System.String"))
            {
                propertyInfo.SetValue(entity, fieldValue);
            }

            if (isType(propertyInfo.FieldType, "System.Boolean"))
            {
                bool value;
                if (fieldValue.Equals("0") || fieldValue.ToLower().Equals("false"))
                {
                    value = false;
                }
                else
                {
                    value = true;
                }
                propertyInfo.SetValue(entity, value);
            }

            if (isType(propertyInfo.FieldType, "System.Int32"))
            {
                if (fieldValue != "")
                    propertyInfo.SetValue(entity, int.Parse(fieldValue));
                else
                    propertyInfo.SetValue(entity, 0);

            }

            if (isType(propertyInfo.FieldType, "System.Decimal"))
            {
                if (fieldValue != "")
                    propertyInfo.SetValue(entity, Decimal.Parse(fieldValue));
                else
                    propertyInfo.SetValue(entity, new Decimal(0));

            }

            if (isType(propertyInfo.FieldType, "System.Nullable`1[System.DateTime]"))
            {
                if (fieldValue != "")
                {
                    try
                    {
                        propertyInfo.SetValue(
                            entity,
                            (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd HH:mm:ss", null));
                    }
                    catch
                    {
                        propertyInfo.SetValue(entity, (DateTime?)DateTime.ParseExact(fieldValue, "yyyy-MM-dd", null));
                    }
                }
                else
                    propertyInfo.SetValue(entity, null);

            }
        }



        /// <summary>
        /// 获取相应属性的值
        /// </summary>
        /// <param name="entity">实体</param>
        /// <param name="fieldName">属性名</param>
        public static string getValue(object entity, string fieldName)
        {
            string value = null;
            Type entityType = entity.GetType();

            FieldInfo propertyInfo = entityType.GetField(fieldName);

            if (isType(propertyInfo.FieldType, "System.String"))
            {
                value = propertyInfo.GetValue(entity).ToString();
            }

            if (isType(propertyInfo.FieldType, "System.Boolean"))
            {
                if (propertyInfo.GetValue(entity) != null)
                {
                    Boolean t = (Boolean)propertyInfo.GetValue(entity);
                    if (t)
                    {
                        value = "1";
                    }
                    else
                    {
                        value = "0";
                    }
                }
                else
                {
                    value = null;
                }
            }

            if (isType(propertyInfo.FieldType, "System.Int32"))
            {
                if (propertyInfo.GetValue(entity) != null)
                {
                    value = propertyInfo.GetValue(entity).ToString();
                }

            }

            if (isType(propertyInfo.FieldType, "System.Decimal"))
            {
                if (propertyInfo.GetValue(entity) != null)
                {
                    value = propertyInfo.GetValue(entity).ToString();
                }

            }

            if (isType(propertyInfo.FieldType, "System.Nullable`1[System.DateTime]"))
            {
                if (propertyInfo.GetValue(entity) != null)
                {
                    value = propertyInfo.GetValue(entity).ToString();
                }
            }

            return value;
        }


        /// <summary>
        /// 类型匹配
        /// </summary>
        /// <param name="type"></param>
        /// <param name="typeName"></param>
        /// <returns></returns>
        public static bool isType(Type type, string typeName)
        {
            if (type.ToString() == typeName)
                return true;
            if (type.ToString() == "System.Object")
                return false;

            return isType(type.BaseType, typeName);
        }

















    }
}
